<?php


namespace App\Modules\System\Criterias;


use Flower\Core\Repository\Contracts\RepositoryInterface;
use Flower\Core\Repository\Criterias\BaseCriterias;

class GetMyVisitorListCriteria extends BaseCriterias
{
    private $params;


    public function __construct($user_id, $store_id, $startDate, $endDate, $isLimit = false)
    {
        $this->params = [
            'user_id' => $user_id,
            'store_id' => $store_id,
            'start_date' => $startDate,
            'end_date' => $endDate,
            'is_limit' => $isLimit
        ];
    }

    public function apply($model, RepositoryInterface $repository)
    {

        $model = $this->params['is_limit'] ?

            $model->with('member:id,name,face')
                ->where('user_id', '=', $this->params['user_id'])
                ->where('store_id', '=', $this->params['store_id'])
                ->whereDate('visit_time', '>=', $this->params['end_date'])
                ->whereDate('visit_time', '<=', $this->params['start_date'])
                ->orderByDesc('visit_time')->take(5)
            :
            $model->with('member:id,name,face')
                ->where('user_id', '=', $this->params['user_id'])
                ->where('store_id', '=', $this->params['store_id'])
                ->whereDate('visit_time', '>=', $this->params['end_date'])
                ->whereDate('visit_time', '<=', $this->params['start_date'])
                ->orderByDesc('visit_time');
        return $model;
    }


}
